import {getNextCategoryAPI} from '@/api/user'
import {useRoute} from 'vue-router'
import {ref} from 'vue'
import { onBeforeRouteUpdate } from 'vue-router'

export const useCategory=()=>{
    // 目标:路由参数变化的时候可以把分类数据接口重新调用
    // 1.监听路由参数变化
    onBeforeRouteUpdate((to)=>{
    // 存在问题:使用最新的路由参数请求数据
    getNextCategory(to.params.id)
  })
    const route = useRoute()
    const NextCategory = ref([])
    const getNextCategory = async (id) => {
        const res = await getNextCategoryAPI(id)
        // console.log(res.data.result);
        NextCategory.value = res.data.result
      }
      getNextCategory(route.params.id)
      return {NextCategory}
}